﻿
<table class="layui-table" id="table-title" style="margin: 0 10px 0 10px; width: 96%;text-align: center">
   
    <thead >
    <tr >
        <th style="text-align: center">试卷名称</th>
        <th style="text-align: center">出卷人</th>
        <th style="text-align: center">开始时间</th>
        <th style="text-align: center">结束时间</th>
        <th style="text-align: center">限时/分钟</th>
        <th style="text-align: center">成绩</th>
        <th></th>
    </tr>
    <thead>

</table>

@*<script src="~/Scripts/TestPaper/TestFunction.js"></script>*@
<script>
    $("div").on("click", ".examTest", function () {
        var e = $(this).attr("button-Item");
        window.location.assign("/AnswerInfoTb/Examination/" + e);
    });
    var str = window.location.href;
    var Index = str.lastIndexOf('/');
    var cid = str.substring(Index + 1);

    getTest();
    // 获取试卷
    function getTest() {
        $.ajax({
            async: false,
            url: '/AnswerInfoTb/ResultAllTestToStu',
            type: 'get',
            data: {
                curriculaId: cid
            },
            dataType: 'json',
            success: function (data) {
                for (let i = 0; i <data.length; i++) {
                    ShowTest(i,data);
                }
            }
        });
    };
    function ShowTest(id, testData) {
        var testId = testData[id].testId;
        //if (testData[id].status === "未发布") { }
        //else if (testData[id].status === "发布中") {
            let str = `<button type="button" class="layui-btn layui-btn-normal layui-btn-sm examTest" button-Item="${testId}" id="btn${testId}">开始答题</button>`;
            
            if (testData[id].status === 3) {
                str += `<a href="/AnswerInfoTb/Recommend/${testId}" class="layui-btn layui-btn-primary layui-btn-sm" style="text-decoration:none" >查看错题重点</a>`;
            }
            
            let limitTime="不限时";
            if (testData[id].limitTime !== 0) {
                limitTime = testData[id].limitTime;
            }
            var startTime = testData[id].startTime;
            var endTime = testData[id].endTime;
            const items = `
                            <tr>
                            <td>
                                <span id="TestName_${testId}">${testData[id].testName}</span>
                            </td>
                            <td>
                                <span id="Creator_${testId}">${testData[id].author}</span>
                            </td>
                            <td>
                                <span id="Start_${testId}">${startTime}</span>
                            </td>
                            <td>
                                <span id="End_${testId}">${endTime}</span>
                            </td>
                            <td>
                                <span id="Limit_${testId}">${limitTime}</span>
                            </td>
                            <td>
                                <span id="Score_${testId}">${testData[id].score}</span>
                            </td>
                            <td>${str}</td>
                        </tr>`;
            $("#table-title").append(items);
            if (JudgeTime(startTime, endTime,testId) === false) {
                $(`#btn${testId}`).attr("disabled", "disabled");
                $(`#btn${testId}`).attr("class", "layui-btn layui-btn-primary layui-btn-sm");
            }
            if (testData[id].hadTest) {
                $(`#btn${testId}`).text("已完成");
                $(`#btn${testId}`).attr("disabled", "disabled");
                $(`#btn${testId}`).attr("class", "layui-btn layui-btn-sm");
            }

        //} 

    }

    function JudgeTime (beginDateStr, endDateStr,id) { //判断时间
        var curDate = new Date(),
            beginDate = new Date(beginDateStr),
            endDate = new Date(endDateStr);
        if (curDate >= beginDate && curDate <= endDate) {
            return true;
        }
        if (curDate < beginDate)
            $(`#btn${id}`).text("未开始");
        else if (curDate > beginDate){
            $(`#btn${id}`).text("已结束");
        }
        
        return false;
    }


</script>